<p>
A channel lets Nuntium communicate with the world.<br/>
By "the world" we mean anything that's not your particular application. For example: cellphone companies, email clients and twitter. <a href="http://code.google.com/p/nuntium/wiki/Channels" target="_blank">Learn more...</a>
</p>

<table class="table" cellspacing="4" id="channels" width="100%">
  <tr>
    <td colspan="7" style="background:white"><%= render 'shared/create_channel' -%></td>
    <%- unless channels.empty? -%>
      <%
      channel_kinds = [['All', '']] + Channel.kinds
      channel_kinds.map!{|x| x[0] << ' channels'; x}
      channel_kinds = channel_kinds.select{|x| x[1] == '' || channels.any?{|c| c.kind == x[1]}}
      -%>
      <%- if channel_kinds.length > 2 -%>
        <td colspan="3" align="right" style="background:white">Show:
          <%= select_tag('kind', options_for_select(channel_kinds), :onchange => 'filter_channels_by_kind(this)') -%>
        </td>
      <%- end -%>
    <%- end -%>
  </tr>
  <%- unless channels.empty? -%>
    <tr>
      <th>Name</th>
      <th>Kind</th>
      <th>Direction</th>
      <th>Protocol</th>
      <th>Info</th>
      <th>Priority</th>
      <th>Application</th>
      <th>Queued AO Messages</th>
      <th></th>
    </tr>
    <%- channels.each_with_index do |chan, i| -%>
      <tr class="<%= i.even_to_s -%>" id="chan-<%= chan.id %>">
        <td>
          <%- status = chan.paused ? 'paused' : (chan.enabled ? 'enabled' : 'disabled') -%>
          <%= image_tag "#{status}.png", :size => '16x16', :style => 'position:relative; top:4px', :title => "This channel is #{status}", :class => "img" -%>
          <%- if chan.has_connection? -%><%= image_tag "#{connected_by_channel_id[chan.id] ? 'connected' : 'disconnected'}.png", :size => '16x16', :style => 'position:relative; top:4px', :title => "This channel is #{chan.connected? ? 'connected' : 'disconnected'}" -%><%- end -%>
          <b><%= chan.name -%></b>
        </td>
        <td><%= chan.kind -%></td>
        <td><%= chan.direction_text -%></td>
        <td><%= chan.protocol -%></td>
        <td><%= chan.info.blank? ? '' : chan.info.html_safe -%></td>
        <td><%= chan.priority -%></td>
        <td><%= chan.application.try(:name) -%></td>
        <%-
        queued = queued_ao_messages_count_by_channel_id[chan.id]
        if queued > 0
          -%>
          <td align="right"><a href="/ao_messages?search=<%= CGI.escape "channel:#{chan.name} state:queued" %>"><%= queued -%></a></td>
          <%-
        else
          -%>
          <td align="right">0</td>
          <%-
        end
        -%>
        <td>
          <a href="/logs?search=<%= CGI.escape "channel:#{chan.name}" %>">view logs</a>
          <a href="/ao_messages?search=<%= CGI.escape "channel:#{chan.name}" %>">view aos</a>
          <a href="/at_messages?search=<%= CGI.escape "channel:#{chan.name}" %>">view ats</a>
          <% unless logged_in_application && chan.application_id != logged_in_application.id %>
            <%
            [
              ['resume', chan.paused?],
              ['disable', !chan.paused? && chan.enabled?],
              ['pause', !chan.paused? && chan.enabled?],
              ['enable', !chan.paused? && !chan.enabled?]
            ].each do |name, shown|
              %>
              <a onclick="<%= name %>_channel(<%= chan.id %>, '<%= chan.name %>'); return false;" class="<%= name %>" style="<%= shown ? '' : 'display:none' %>" href="#"><%= name %></a>
              <%
            end
            %>
            <a href="/channels/<%= chan.id %>/edit">edit</a>
            <a href="/channels/<%= chan.id %>" data-method="delete" data-confirm="Are you sure you want to delete the channel <%= chan.name %>?">delete</a>
          <% end %>
        </td>
      </tr>
    <%- end -%>
  <%- end -%>
</table>
